package com.ridanlabs.onelist.db;

import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;

import javax.jdo.annotations.Element;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

import com.ridanlabs.onelist.rpclib.Configuration;

@PersistenceCapable
public final class UserModel {
	
	public UserModel(String username, String password) {
		this.key = KeyFactory.createKey(UserModel.class.getSimpleName(), username);
		
		this.username = username;
		this.password = password;
	}
	
	public Key getKey() {
		return key;
	}
	
	public String getUsername() {
		return this.username;
	}
	
	public String getPassword() {
		return this.password;
	}

	public String[] getEmailOfFriends() {
		// TODO Auto-generated method stub
		return null;
	}
	
	public HashSet<Key> getUserLists() {
		return this.lists;
	}

	/**
	 * Changes persist to the list which was passed in through the parameter.
	 */
	public void associateListToUser(ListModel list) {
		lists.add(list.getKey());
		list.getListUsers().add(this.getKey());
	}

	/**
	 * Changes persist to the list which was passed in through the parameter.
	 */
	public void unassociateListFromUser(ListModel list) {
		this.lists.remove(list.getKey());
		list.getListUsers().remove(this.getKey());
	}
	
	public boolean registerDeviceToUser(RegistrationModel regInfo) {
		if( this.registeredDeviceIDs.size() > Configuration.MAX_REGISTERED_DEVICES_PER_USER) 
			return false;
		
		this.registeredDeviceIDs.add(regInfo);
		
		return true;
	}
	
	public void unregisterDeviceFromUser(RegistrationModel regInfo) {
		this.registeredDeviceIDs.remove( regInfo );
	}
	
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key key;
	
	@Persistent
	private String username;
	
	@Persistent
	private String password;
	
	/**
	 * This is a set of String values which represent, in the form of IDs,
	 * all the devices that the user is logged into. Might want to limit the number
	 * of devices that the user is logged into.
	 */
	@Persistent
	@Element(dependent = "true")
	private HashSet<RegistrationModel> registeredDeviceIDs = new HashSet<RegistrationModel>(); //TODO maybe HashSet
	
	@Persistent
	private HashSet<Key> lists = new HashSet<Key>();
}
